from agno.models.openai import OpenAILike

from configs.basic_config import ALI_GENERATION_MODEL, AGENT_URL, ALI_DASHSCOPE_API_KEY
from agno.agent import Agent
from agno.db.postgres import PostgresDb

from agno.os import AgentOS
from agno.tools import tool

# 数据库连接
#db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
#db = PostgresDb(db_url=db_url)


@tool(requires_confirmation=True)
def delete_records(table_name: str, count: int) -> str:
    """从数据库表中删除记录。

    Args:
        table_name: 表名
        count: 要删除的记录数

    Returns:
        str: 确认消息
    """
    return f"已从 {table_name} 删除 {count} 条记录"


@tool(requires_confirmation=True)
def send_notification(recipient: str, message: str) -> str:
    """向用户发送通知。

    Args:
        recipient: 收件人的邮箱或用户名
        message: 通知消息

    Returns:
        str: 确认消息
    """
    return f"已发送通知给 {recipient}: {message}"


# 创建具有 HITL 工具的代理
agent = Agent(
    name="数据管理员", # 翻译名称
    id="data_manager",
    # --- 修改模型参数 ---
    model=OpenAILike(id=ALI_GENERATION_MODEL, name=ALI_GENERATION_MODEL, api_key=ALI_DASHSCOPE_API_KEY, base_url=AGENT_URL),
    tools=[delete_records, send_notification],
    instructions=["你帮助用户管理数据操作"], # 翻译提示词
    #db=db,
    markdown=True,
)

# 创建 AgentOS
agent_os = AgentOS(
    id="agentos-hitl",
    agents=[agent],
)

app = agent_os.get_app()

if __name__ == "__main__":
    #api test:
    #http://localhost:7777/agents/data_manager/runs
    #http://localhost:7777/agents/data_manager/runs/{run_id}/continue
    agent_os.serve(app="hitl_confirmation:app", port=7777)